package com.example.shop.controller;

import com.example.shop.entity.Productstype;
import com.example.shop.service.ProductstypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName ProductstypeController
 * @Description TODO
 * @Author 萍
 * @Date 2025/3/26 10:25
 */

@RestController
@RequestMapping("/api/producttypes")
public class ProductstypeController {

    @Autowired
    private ProductstypeService productstypeService;

    /**
     * 通过ID查询单条数据
     *
     * @param productstypeid 主键
     * @return 实例对象
     */
    @GetMapping("/{productstypeid}")
    public ResponseEntity<Productstype> queryById(@PathVariable Integer productstypeid) {
        Productstype productstype = productstypeService.queryById(productstypeid);
        if (productstype == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(productstype);
    }

    /**
     * 新增数据
     *
     * @param productstype 实例对象
     * @return 实例对象
     */
    @PostMapping
    public ResponseEntity<Productstype> insert(@RequestBody Productstype productstype) {
        Productstype insertedProductstype = productstypeService.insert(productstype);
        return ResponseEntity.ok(insertedProductstype);
    }

    /**
     * 修改数据
     *
     * @param productstype 实例对象
     * @return 实例对象
     */
    @PutMapping
    public ResponseEntity<Productstype> update(@RequestBody Productstype productstype) {
        Productstype updatedProductstype = productstypeService.update(productstype);
        return ResponseEntity.ok(updatedProductstype);
    }

    /**
     * 通过主键删除数据
     *
     * @param productstypeid 主键
     * @return 是否成功
     */
    @DeleteMapping("/{productstypeid}")
    public ResponseEntity<Void> deleteById(@PathVariable Integer productstypeid) {
        boolean isDeleted = productstypeService.deleteById(productstypeid);
        if (isDeleted) {
            return ResponseEntity.noContent().build();
        }
        return ResponseEntity.notFound().build();
    }
}
